<template>
  <div class="edit">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{detail ? '院校详情': '院校管理'}} <span v-if="form.id">【{{form.schoolName}}】</span> </span>
        <el-button @click="$router.go(-1)" style="float: right; padding: 3px 0" type="text">返回</el-button>
      </div>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="中文名字" prop="schoolName" :rules="{required: true, message: '请输入中文名字'}">
          <el-input :readonly="detail" v-model="form.schoolName" placeholder="请输入中文名字"/>
        </el-form-item>
        <el-form-item label="院校编码" prop="schoolCode">
          <el-input :readonly="detail" v-model="form.schoolCode" placeholder="请输入院校编码"/>
        </el-form-item>
        <el-form-item label="名校风采展示" prop="showOrNot" :rules="{required: true, message: '请输入中文名字'}">
          <el-radio-group v-model="form.showOrNot">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所在省份" prop="provinceName">
          <el-select :disabled="detail" v-model="form.provinceName" filterable clearable size="small"
                     placeholder="请选择所在省份">
            <el-option
              v-for="item in provinceName"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="schoolAddress">
          <el-input :readonly="detail" v-model="form.schoolAddress" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="所属机构" prop="agencyName">
          <el-input :readonly="detail" v-model="form.agencyName" placeholder="请输入所属机构"/>
        </el-form-item>
        <el-form-item label="办学层次" prop="uorS">
          <el-select :disabled="detail" v-model="form.uorS" clearable size="small" placeholder="请选择本科专科">
            <el-option
              v-for="item in education"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排名" prop="schoolRank">
          <el-input :readonly="detail" v-model="form.schoolRank" type="number" placeholder="请输入排名"/>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <div class="upload-list">
            <Upload v-model="form.imgUrl" @change="uploadChange"/>
          </div>
          <!--<el-input v-model="form.imgUrl" placeholder="请输入图片"/>-->
        </el-form-item>
        <!--<el-form-item label="图片ID" prop="imgId">
          <el-input v-model="form.imgId" placeholder="请输入图片ID"/>
        </el-form-item>-->
        <el-form-item label="学校类型" prop="schoolType">
          <el-input v-model="form.schoolType" :readonly="detail" placeholder="请输入学校类型"/>
        </el-form-item>
        <el-form-item label="标签" prop="typeId">
          <el-checkbox-group v-model="form.typeId">
            <el-checkbox v-for="item in schoolType"
                         :label="item.dictLabel"
                         :key="item.dictLabel">
              {{item.dictLabel}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="招办电话" prop="recruitTel">
          <el-input v-model="form.recruitTel" :readonly="detail" type="text" placeholder="请输入招办电话"/>
        </el-form-item>
        <el-form-item label="官网" prop="officialWeb">
          <el-input v-model="form.officialWeb" :readonly="detail" placeholder="请输入官网"/>
        </el-form-item>
        <el-form-item label="硕士点" prop="masterPoint">
          <el-input v-model="form.masterPoint" :readonly="detail" type="number" placeholder="请输入硕士点"/>
        </el-form-item>
        <el-form-item label="博士点" prop="doctorPoint">
          <el-input v-model="form.doctorPoint" :readonly="detail" type="number" placeholder="请输入博士点"/>
        </el-form-item>
        <el-form-item label="院系设置" prop="facultySetting">
          <el-input v-model="form.facultySetting" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入院系设置"/>
        </el-form-item>
        <el-form-item label="院校介绍" prop="schoolDesc">
          <el-input v-model="form.schoolDesc" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入院校介绍"/>
        </el-form-item>
        <el-form-item label="学校领导" prop="schoolLeader">
          <el-input v-model="form.schoolLeader" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入学校领导"/>
        </el-form-item>
        <el-form-item label="食堂情况" prop="diningHall">
          <el-input v-model="form.diningHall" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入食堂情况"/>
        </el-form-item>
        <el-form-item label="平均伙食标准" prop="foodStandard">
          <el-input v-model="form.foodStandard" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入平均伙食标准"/>
        </el-form-item>
        <el-form-item label="宿舍情况" prop="hostelDesc">
          <el-input v-model="form.hostelDesc" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入宿舍情况"/>
        </el-form-item>
        <el-form-item label="学校环境" prop="schoolCondition">
          <el-input v-model="form.schoolCondition" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入学校环境"/>
        </el-form-item>
        <el-form-item label="对艺术类招生的录取办法" prop="artAdmissions">
          <el-input v-model="form.artAdmissions" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入对艺术类招生的录取办法"/>
        </el-form-item>
        <el-form-item label="奖学金介绍" prop="scholarship">
          <el-input v-model="form.scholarship" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入奖学金介绍"/>
        </el-form-item>
        <el-form-item label="对少数民族考生的特色政策" prop="minorityCandidates">
          <el-input v-model="form.minorityCandidates" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入对少数民族考生的特色政策"/>
        </el-form-item>
        <el-form-item label="困难生资助办法" prop="difficultStudent">
          <el-input v-model="form.difficultStudent" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入困难生资助办法"/>
        </el-form-item>
        <el-form-item label="对往届生的录取政策" prop="pastStudents">
          <el-input v-model="form.pastStudents" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入对往届生的录取政策"/>
        </el-form-item>

        <el-form-item label="转专业的实施办法" prop="changeMajor">
          <el-input v-model="form.changeMajor" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入转专业的实施办法"/>
        </el-form-item>
        <el-form-item label="录取批次安排" prop="admissionBatch">
          <el-input v-model="form.admissionBatch" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入录取批次安排"/>
        </el-form-item>
        <el-form-item label="加分政策" prop="bonusPolicy">
          <el-input v-model="form.bonusPolicy" :readonly="detail" :autosize="{minRows: 4}" type="textarea"
                    placeholder="请输入加分政策"/>
        </el-form-item>

        <el-form-item v-if="!detail">
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveCode">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  import {
    listCompassUniversityInfo,
    getCompassUniversityInfo,
    delCompassUniversityInfo,
    addCompassUniversityInfo,
    updateCompassUniversityInfo,
    exportCompassUniversityInfo
  } from "@/api/system/CompassUniversityInfo";
  import dictMixins from "@/views/mixins/dictMixins";
  import {getQueryObject} from "@/utils";
  import Upload from "@/components/Upload";

  export default {
    mixins: [dictMixins],

    components: {
      Upload
    },

    data() {
      return {
        form: {
          typeId: [],
          imageUrl: ''
        },
        schoolType: [],
        provinceName: [],
        education: [],
        detail: false
      }
    },

    async mounted() {
      let {id, detail} = getQueryObject()
      if (id) {
        this.getInfo(id)
      }
      if (detail) {
        this.detail = true
      }
      this.schoolType = await this.getDictByTypeName('typeIds')
      this.provinceName = await this.getDictByTypeName('provinceName')
      this.education = await this.getDictByTypeName('education')
    },

    methods: {
      // 保存
      saveCode() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.typeId.length) {
              var typeIds = JSON.parse(JSON.stringify(this.form.typeId))
              this.form.typeIds = typeIds.join(' ')
            } else {
              this.form.typeIds = ''
            }
            if (this.form.id != null) {
              updateCompassUniversityInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.$router.go(-1);
                }
              });
            } else {
              addCompassUniversityInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.$router.go(-1);
                }
              });
            }
          } else {
            this.$nextTick(() => {
              let isError = document.getElementsByClassName('is-error')
              isError[0].scrollIntoView({
                block: 'center',
                behavior: 'smooth',
              })
            })
          }
        });
      },

      // 计算优惠券时间
      getCouponTime() {
        let {validType, validNum} = this.addform
        let nowStamp = new Date().getTime()
        let endStamp = 0
        switch (validType) {
          case "day":
            endStamp = nowStamp + validNum * 1000 * 60 * 60 * 24
            break;
          case "month":
            endStamp = nowStamp + validNum * 1000 * 60 * 60 * 24 * 30
            break;
          case "year":
            endStamp = nowStamp + validNum * 1000 * 60 * 60 * 24 * 365
            break
        }
        return formatDate(endStamp)
      },

      // 编辑时候获取优惠券信息回显
      getInfo(id) {
        getCompassUniversityInfo(id).then(response => {
          response.data.typeId = response.data.typeIds.split(' ')
          this.form = response.data
          console.log(this.form.imgUrl)
        });
      },

      uploadChange(res) {
        console.log(res)
      },

    }
  }
</script>

<style scoped>
  .el-input {
    width: 60%;
  }

  .el-textarea {
    width: 80%;
  }
</style>
